package com.ray.friendslist.adaptor;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ray.friendslist.ImageViewActivity;
import com.ray.friendslist.R;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.feeds.FeedEntyty;
import com.ray.friendslist.images.ImageLoader;

public class FeedCursorAdapter extends CursorAdapter implements
		OnItemClickListener {

	public FeedCursorAdapter(Context context, Cursor c) {
		super(context, c, true);
	}

	private TextView mTextView1;
	private TextView mTextView2;
	private ImageView mImageView;
	private TextView dateTextView;
	private TextView likesTextView;
	private ExpandableHeightGridView mGridView;
	private ImageAdapter mImageAdapter;

	@Override
	public void bindView(View view, Context ctx, Cursor cursor) {
		// TODO Auto-generated method stub
		initViews(view);
		likesTextView.setText("likes: "
				+ cursor.getString(cursor
						.getColumnIndex(FriendsContract.VKFeedColoumns.LIKES)));
		mTextView2.setVisibility(TextView.VISIBLE);
		dateTextView.setText(cursor.getString(cursor
				.getColumnIndex(FriendsContract.VKFeedColoumns.DATE)));
		mTextView1.setText(cursor.getString(cursor
				.getColumnIndex(FriendsContract.VKFeedColoumns.PROFILE_NAME)));
		mTextView2.setText(cursor.getString(cursor
				.getColumnIndex(FriendsContract.VKFeedColoumns.TEXT)));
		if (TextUtils.isEmpty(mTextView2.getText().toString())) {
			mTextView2.setVisibility(TextView.GONE);
		}
		setImage(cursor.getString(cursor
				.getColumnIndex(FriendsContract.VKFeedColoumns.PROFILE_PHOTO)),
				mImageView);
		mGridView.setExpanded(true);
		mImageAdapter = new ImageAdapter(
				ctx,
				FeedEntyty.convertFeedAttachmentsArray(cursor.getString(cursor
						.getColumnIndex(FriendsContract.VKFeedColoumns.ATTACHMENT_PHOTO))));
		mGridView.setAdapter(mImageAdapter);
		mGridView.setOnItemClickListener(this);
	}

	private void initViews(View view) {
		mTextView1 = (TextView) view.findViewById(R.id.vkFeedTv1);
		mTextView2 = (TextView) view.findViewById(R.id.vkFeedTv2);
		mImageView = (ImageView) view.findViewById(R.id.Vk_Feed_Profile_ImView);
		dateTextView = (TextView) view.findViewById(R.id.vkFeed_date_textView);
		likesTextView = (TextView) view
				.findViewById(R.id.vkFeed_likes_textView);
		mGridView = (ExpandableHeightGridView) view
				.findViewById(R.id.feed_images_layout);
	}

	@Override
	public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
		View view = View.inflate(arg0, R.layout.vk_feed_adapter, null);
		return view;
	}

	private void setImage(final String source, final ImageView view) {
		if (view.getVisibility() == ImageView.GONE) {
			view.setVisibility(ImageView.VISIBLE);
		}
		if (!TextUtils.isEmpty(source)) {
			ImageLoader.getInstance().bind(this, view, source);
		} else {
			view.setImageBitmap(null);
			view.setVisibility(ImageView.GONE);
		}
	}

	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		String value = (String) adapterView.getItemAtPosition(position);
		Intent intent = new Intent(view.getContext(), ImageViewActivity.class);
		intent.putExtra("IMAGE_SOURCE", value);
		view.getContext().startActivity(intent);
	}

	private class ImageAdapter extends BaseAdapter {

		private String[] mSource;
		private Context mContext;
		private ImageView mImageView;

		public ImageAdapter(Context context, String[] source) {
			mSource = source;
			mContext = context;
			mImageView = new ImageView(mContext);
		}

		public int getCount() {
			return mSource.length;
		}

		public Object getItem(int position) {
			return mSource[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			mImageView = new ImageView(mContext);
			if (mSource[position].compareTo("") == 0) {
				mImageView.setImageBitmap(null);
				return mImageView;
			}
			ImageLoader.getInstance().bind(this, mImageView, mSource[position]);
			// mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			// TODO not hardcode 100, 100 convert to dp
			mImageView.setLayoutParams(new GridView.LayoutParams(100, 100));
			return mImageView;
		}
	}
}
